/*
Copyright (c) <2007> <Juan Pablo Califano (jpcalifano@gmail.com)>

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
*/

using System;
using System.Windows.Forms;
using System.Collections;
using System.IO;

using ar.com.califa010.Controls;
using ar.com.califa010.ASClassExporter;
using ar.com.califa010.ASClassExporter.Events;
using ar.com.califa010.ASClassExporter.SwfDataLoader;
using ar.com.califa010.AS3ClassExporter_Front;

namespace ar.com.califa010.AS3ClassExporter_Front {
	/// <summary>
	/// Description of MainForm.	
	/// </summary>
	public class MainForm : System.Windows.Forms.Form {

	//	private System.Windows.Forms.TextBox textBox1;
		public	System.Windows.Forms.TextBox textBox1;
		private System.Windows.Forms.Button button2;
		private System.Windows.Forms.OpenFileDialog fileDialog;
		private System.Windows.Forms.FolderBrowserDialog outputFolderDialog;
		private System.Windows.Forms.GroupBox classPathGroup;
		private FolderListControl classPathControl;
		private System.Windows.Forms.GroupBox outputFolderGroup;
		private System.Windows.Forms.TextBox txtOutputDir;
		private System.Windows.Forms.Button cmdBrowseOutput;
		
		private SwfExporter swfExporter;

		private System.Windows.Forms.MainMenu mainMenu1;
		private System.Windows.Forms.MenuItem menuItem1;
		private System.Windows.Forms.MenuItem cmdLoadConfig;
		private System.Windows.Forms.MenuItem cmdSaveConfig;
		private System.Windows.Forms.MenuItem cmdQuit;
		private System.Windows.Forms.OpenFileDialog openCfgDialog;
		private System.Windows.Forms.SaveFileDialog saveCfgDialog;
		private FileListControl swfListControl;
		private System.Windows.Forms.GroupBox swfListGroup;
		private System.Windows.Forms.MenuItem menuItem2;
		private System.Windows.Forms.MenuItem cmdAbout;
		private ConfigSettings configSettings;

		private AboutForm aboutForm;

		public MainForm() {
			//
			// The InitializeComponent() call is required for Windows Forms designer support.
			//
			InitializeComponent();
			
			//
			// TODO: Add constructor code after the InitializeComponent() call.
			//
			//
			//
			aboutForm				= new AboutForm();


			swfExporter				= new SwfExporter(this,SwfDataLoaderType.AVM_2);
			configSettings			= new ConfigSettings();


			swfExporter.OnInit		+= new SwfExporter.SwfExporterEventHandler(handleOnInit);
			swfExporter.OnFinish	+= new SwfExporter.SwfExporterEventHandler(handleOnFinish);
			swfExporter.OnError		+= new SwfExporter.SwfExporterEventHandler(handleOnError);
			swfExporter.OnStatus	+= new SwfExporter.SwfExporterEventHandler(handleOnStatus);

		}
		
		private void handleOnInit(object sender, StatusEventArgs e) {
			textBox1.AppendText(e.status + "\r\n\r\n");
		}
		private void handleOnStatus(object sender, StatusEventArgs e) {
			textBox1.AppendText(e.status + "\r\n\r\n");
		}
		private void handleOnFinish(object sender, StatusEventArgs e) {
            FinishEventArgs fe = (FinishEventArgs) e;

			textBox1.AppendText(fe.status + "\r\n");
			textBox1.AppendText("log file created: "  + fe.logFile + "\r\n");
		}
		
		private void handleOnError(object sender, StatusEventArgs e) {
			textBox1.AppendText(e.status + "\r\n\r\n");
		}


		[STAThread]
		public static void Main(string[] args) {
			Application.Run(new MainForm());
		}
		
		#region Windows Forms Designer generated code
		/// <summary>
		/// This method is required for Windows Forms designer support.
		/// Do not change the method contents inside the source code editor. The Forms designer might
		/// not be able to load this method if it was changed manually.
		/// </summary>
		private void InitializeComponent() {
			this.textBox1 = new System.Windows.Forms.TextBox();
			this.button2 = new System.Windows.Forms.Button();
			this.fileDialog = new System.Windows.Forms.OpenFileDialog();
			this.outputFolderDialog = new System.Windows.Forms.FolderBrowserDialog();
			this.classPathGroup = new System.Windows.Forms.GroupBox();
			this.classPathControl = new ar.com.califa010.Controls.FolderListControl();
			this.outputFolderGroup = new System.Windows.Forms.GroupBox();
			this.txtOutputDir = new System.Windows.Forms.TextBox();
			this.cmdBrowseOutput = new System.Windows.Forms.Button();
			this.mainMenu1 = new System.Windows.Forms.MainMenu();
			this.menuItem1 = new System.Windows.Forms.MenuItem();
			this.cmdLoadConfig = new System.Windows.Forms.MenuItem();
			this.cmdSaveConfig = new System.Windows.Forms.MenuItem();
			this.cmdQuit = new System.Windows.Forms.MenuItem();
			this.menuItem2 = new System.Windows.Forms.MenuItem();
			this.cmdAbout = new System.Windows.Forms.MenuItem();
			this.openCfgDialog = new System.Windows.Forms.OpenFileDialog();
			this.saveCfgDialog = new System.Windows.Forms.SaveFileDialog();
			this.swfListControl = new ar.com.califa010.Controls.FileListControl();
			this.swfListGroup = new System.Windows.Forms.GroupBox();
			this.SuspendLayout();
			// 
			// textBox1
			// 
			this.textBox1.BackColor = System.Drawing.SystemColors.Window;
			this.textBox1.Font = new System.Drawing.Font("Microsoft Sans Serif", 12F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.World);
			this.textBox1.Location = new System.Drawing.Point(552, 0);
			this.textBox1.Multiline = true;
			this.textBox1.Name = "textBox1";
			this.textBox1.ReadOnly = true;
			this.textBox1.ScrollBars = System.Windows.Forms.ScrollBars.Vertical;
			this.textBox1.Size = new System.Drawing.Size(256, 296);
			this.textBox1.TabIndex = 0;
			this.textBox1.Text = "";
			// 
			// button2
			// 
			this.button2.FlatStyle = System.Windows.Forms.FlatStyle.System;
			this.button2.Font = new System.Drawing.Font("Microsoft Sans Serif", 12F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.World);
			this.button2.Location = new System.Drawing.Point(400, 512);
			this.button2.Name = "button2";
			this.button2.Size = new System.Drawing.Size(144, 28);
			this.button2.TabIndex = 2;
			this.button2.Text = "Export";
			this.button2.Click += new System.EventHandler(this.InvokeFlashLoadSwf);
			// 
			// fileDialog
			// 
			this.fileDialog.Filter = "archivos swf (*.swf)|*.swf";
			this.fileDialog.Multiselect = true;
			// 
			// classPathGroup
			// 
			this.classPathGroup.Font = new System.Drawing.Font("Microsoft Sans Serif", 12F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.World);
			this.classPathGroup.Location = new System.Drawing.Point(16, 16);
			this.classPathGroup.Name = "classPathGroup";
			this.classPathGroup.Size = new System.Drawing.Size(528, 200);
			this.classPathGroup.TabIndex = 12;
			this.classPathGroup.TabStop = false;
			this.classPathGroup.Text = "ClassPath";
			// 
			// classPathControl
			// 
			this.classPathControl.Location = new System.Drawing.Point(24, 32);
			this.classPathControl.Name = "classPathControl";
			this.classPathControl.Size = new System.Drawing.Size(512, 176);
			this.classPathControl.TabIndex = 13;
			// 
			// outputFolderGroup
			// 
			this.outputFolderGroup.Font = new System.Drawing.Font("Microsoft Sans Serif", 12F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.World);
			this.outputFolderGroup.Location = new System.Drawing.Point(16, 432);
			this.outputFolderGroup.Name = "outputFolderGroup";
			this.outputFolderGroup.Size = new System.Drawing.Size(528, 64);
			this.outputFolderGroup.TabIndex = 14;
			this.outputFolderGroup.TabStop = false;
			this.outputFolderGroup.Text = "Output Folder";
			// 
			// txtOutputDir
			// 
			this.txtOutputDir.Font = new System.Drawing.Font("Microsoft Sans Serif", 12F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.World);
			this.txtOutputDir.Location = new System.Drawing.Point(184, 456);
			this.txtOutputDir.Name = "txtOutputDir";
			this.txtOutputDir.Size = new System.Drawing.Size(320, 21);
			this.txtOutputDir.TabIndex = 16;
			this.txtOutputDir.Text = "";
			this.txtOutputDir.TextChanged += new System.EventHandler(this.txtOutputDir_TextChanged);
			// 
			// cmdBrowseOutput
			// 
			this.cmdBrowseOutput.FlatStyle = System.Windows.Forms.FlatStyle.System;
			this.cmdBrowseOutput.Font = new System.Drawing.Font("Microsoft Sans Serif", 12F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.World);
			this.cmdBrowseOutput.Location = new System.Drawing.Point(24, 456);
			this.cmdBrowseOutput.Name = "cmdBrowseOutput";
			this.cmdBrowseOutput.Size = new System.Drawing.Size(144, 28);
			this.cmdBrowseOutput.TabIndex = 15;
			this.cmdBrowseOutput.Text = "Browse Output";
			this.cmdBrowseOutput.Click += new System.EventHandler(this.cmdBrowseOutput_Click);
			// 
			// mainMenu1
			// 
			this.mainMenu1.MenuItems.AddRange(new System.Windows.Forms.MenuItem[] {
																					  this.menuItem1,
																					  this.menuItem2});
			// 
			// menuItem1
			// 
			this.menuItem1.Index = 0;
			this.menuItem1.MenuItems.AddRange(new System.Windows.Forms.MenuItem[] {
																					  this.cmdLoadConfig,
																					  this.cmdSaveConfig,
																					  this.cmdQuit});
			this.menuItem1.Text = "File";
			// 
			// cmdLoadConfig
			// 
			this.cmdLoadConfig.Index = 0;
			this.cmdLoadConfig.Shortcut = System.Windows.Forms.Shortcut.CtrlO;
			this.cmdLoadConfig.Text = "Load Configuration";
			this.cmdLoadConfig.Click += new System.EventHandler(this.cmdLoadConfig_Click);
			// 
			// cmdSaveConfig
			// 
			this.cmdSaveConfig.Index = 1;
			this.cmdSaveConfig.Shortcut = System.Windows.Forms.Shortcut.CtrlS;
			this.cmdSaveConfig.Text = "Save Configuration";
			this.cmdSaveConfig.Click += new System.EventHandler(this.cmdSaveConfig_Click);
			// 
			// cmdQuit
			// 
			this.cmdQuit.Index = 2;
			this.cmdQuit.Shortcut = System.Windows.Forms.Shortcut.CtrlQ;
			this.cmdQuit.Text = "Quit";
			this.cmdQuit.Click += new System.EventHandler(this.cmdQuit_Click);
			// 
			// menuItem2
			// 
			this.menuItem2.Index = 1;
			this.menuItem2.MenuItems.AddRange(new System.Windows.Forms.MenuItem[] {
																					  this.cmdAbout});
			this.menuItem2.Text = "Help";
			// 
			// cmdAbout
			// 
			this.cmdAbout.Index = 0;
			this.cmdAbout.Shortcut = System.Windows.Forms.Shortcut.CtrlF1;
			this.cmdAbout.Text = "About";
			this.cmdAbout.Click += new System.EventHandler(this.cmdAbout_Click);
			// 
			// openCfgDialog
			// 
			this.openCfgDialog.Filter = "archivos xml (*.xml)|*.xml";
			// 
			// saveCfgDialog
			// 
			this.saveCfgDialog.Filter = "archivos xml (*.xml)|*.xml";
			// 
			// swfListControl
			// 
			this.swfListControl.Location = new System.Drawing.Point(24, 240);
			this.swfListControl.Name = "swfListControl";
			this.swfListControl.Size = new System.Drawing.Size(512, 176);
			this.swfListControl.TabIndex = 18;
			// 
			// swfListGroup
			// 
			this.swfListGroup.Font = new System.Drawing.Font("Microsoft Sans Serif", 12F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.World);
			this.swfListGroup.Location = new System.Drawing.Point(16, 224);
			this.swfListGroup.Name = "swfListGroup";
			this.swfListGroup.Size = new System.Drawing.Size(528, 200);
			this.swfListGroup.TabIndex = 17;
			this.swfListGroup.TabStop = false;
			this.swfListGroup.Text = "Swfs";
			// 
			// MainForm
			// 
			this.AutoScaleBaseSize = new System.Drawing.Size(8, 19);
			this.ClientSize = new System.Drawing.Size(824, 561);
			this.Controls.Add(this.swfListControl);
			this.Controls.Add(this.swfListGroup);
			this.Controls.Add(this.txtOutputDir);
			this.Controls.Add(this.cmdBrowseOutput);
			this.Controls.Add(this.outputFolderGroup);
			this.Controls.Add(this.classPathControl);
			this.Controls.Add(this.classPathGroup);
			this.Controls.Add(this.button2);
			this.Controls.Add(this.textBox1);
			this.Font = new System.Drawing.Font("Microsoft Sans Serif", 12F);
			this.Menu = this.mainMenu1;
			this.Name = "MainForm";
			this.Text = "AS 3.0 Class Exporter";
			this.Load += new System.EventHandler(this.MainForm_Load);
			this.ResumeLayout(false);

		}
		#endregion


		private void MainForm_Load(object sender, System.EventArgs e) {
		}

		void InvokeFlashLoadSwf(object sender, System.EventArgs e) {


			updateConfigFromInterface();

			if(swfExporter.Status == SwfExporter.STATUS_IDDLE) {
				
				textBox1.Text = "";

				swfExporter.InitProcess(configSettings.ClassPathList,
										configSettings.SwfList,
										configSettings.OutputFolder);
			}

		}
		
		private void updateConfigFromInterface() {
			configSettings.ClassPathList	= classPathControl.getItemList();
		//	configSettings.OutputFolder		= outputFolderDialog.SelectedPath;
			configSettings.OutputFolder		= txtOutputDir.Text;
			configSettings.SwfList			= swfListControl.getItemList();

		}

		private void updateConfigFromFile() {
			txtOutputDir.Text = configSettings.OutputFolder;
			outputFolderDialog.SelectedPath = configSettings.OutputFolder;

			swfListControl.setItemList(configSettings.SwfList);
			classPathControl.setItemList(configSettings.ClassPathList);

		}

		private void cmdBrowseSwf_Click(object sender, System.EventArgs e) {
			if(fileDialog.ShowDialog() == DialogResult.OK) {
				
			}
			
		}

		private void cmdBrowseOutput_Click(object sender, System.EventArgs e) {
			if(outputFolderDialog.ShowDialog() == DialogResult.OK) {
				txtOutputDir.Text = outputFolderDialog.SelectedPath;
			}
			
		}

		private void cmdLoadConfig_Click(object sender, System.EventArgs e) {
			if(openCfgDialog.ShowDialog() == DialogResult.OK) {
				if(configSettings.LoadConfig(openCfgDialog.FileName)) {
					updateConfigFromFile();
				}
			}
		}

		private void cmdSaveConfig_Click(object sender, System.EventArgs e) {
			if(saveCfgDialog.ShowDialog() == DialogResult.OK) {
				updateConfigFromInterface();
				configSettings.SaveConfig(saveCfgDialog.FileName);
			}
		}

		private void cmdQuit_Click(object sender, System.EventArgs e) {
			Application.Exit();
		}

		private void cmdAbout_Click(object sender, System.EventArgs e) {
			//	MessageBox.Show("texto","caption");
			//	this.AddOwnedForm(aboutForm);
			aboutForm.ShowDialog(this);
		}

		private void txtOutputDir_TextChanged(object sender, System.EventArgs e) {
		
		}		






	}

}


